#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "qcustomplot.h"
#include <QMessageBox>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    int n;
    QBoxLayout *Vbox0 = new QBoxLayout(QBoxLayout::TopToBottom);
    QBoxLayout *Hbox11 = new QBoxLayout(QBoxLayout::LeftToRight);
    for(n=0;n<MARKERNUM;n++)
    {
        this->markerButton[n] = new QPushButton(QString("Mkr%0").arg(n+1), this);
        this->markerButton[n]->setCheckable(true);
        connect(this->markerButton[n], SIGNAL(toggled(bool)), this, SLOT(markerToggled(bool)));
        Hbox11->addWidget(this->markerButton[n], 0, Qt::AlignLeft);
    }
    Hbox11->addStretch(1);

    QBoxLayout *Hbox12 = new QBoxLayout(QBoxLayout::LeftToRight);
    this->mkrTxtButton = new QPushButton(QString("Enable\nMkrText"), this);
    this->mkrTxtButton->setCheckable(true);
    connect(this->mkrTxtButton, SIGNAL(toggled(bool)), this, SLOT(mkrTxtToggled(bool)));
    Hbox12->addWidget(mkrTxtButton, 0, Qt::AlignLeft);

    this->mMkrToTraceSB = new QSpinBox(this);
    mMkrToTraceSB->setValue(0);
    Hbox12->addWidget(mMkrToTraceSB, 0, Qt::AlignLeft);

    this->logAxisButton = new QPushButton(QString("log axis"), this);
    this->logAxisButton->setCheckable(true);
    connect(this->logAxisButton, SIGNAL(toggled(bool)), this, SLOT(logAxisToggled(bool)));
    Hbox12->addWidget(logAxisButton, 0, Qt::AlignLeft);

    Hbox12->addStretch(1);

    Vbox0->addItem(Hbox11);
    Vbox0->addItem(Hbox12);
    QWidget* myWidget = new QWidget();
    myWidget->setLayout(Vbox0);

    this->customPlot = new HQCustomPlot(this);
    this->customPlot->setInteractions( QCP::iRangeZoom | QCP::iSelectItems);
    this->customPlot->setMouseTracking(false);
    setCentralWidget(customPlot);
    setWindowTitle(tr("Measurement"));
    //init
    this->customPlot->SetMarkerTextEn(false);

    QDockWidget *dock = new QDockWidget(tr("Customers"), this);
    dock->setFeatures(QDockWidget::NoDockWidgetFeatures);
    dock->setAllowedAreas(Qt::BottomDockWidgetArea);
    dock->setWidget(myWidget);
    addDockWidget(Qt::BottomDockWidgetArea, dock);

    //plot curve
    n =50;
    double xScale = 3.0;
    double yScale = 1.0;
    double xOffset = 1;
    double yOffset = 2.5;
    double r1 = 2.0;
    QVector<double> x(n), y(n);
    int i;
    for (i=0; i<n; i++)
    {
      x[i] = M_PI*i/(double)n*xScale + xOffset;
      y[i] = r1*qCos(x[i])*yScale + yOffset;
    }
    this->customPlot->addGraph();
    this->customPlot->graph(0)->setData(x, y);
    this->customPlot->graph(0)->setLineStyle(QCPGraph::lsLine);
    this->customPlot->graph(0)->setPen(QPen(Qt::yellow));
//    this->customPlot->graph(0)->setBrush(Qt::yellow);

    yScale = 2.0;
    yOffset = 1.0;
    QVector<double> x1(n), y1(n);
    for (i=0; i<n; i++)
    {
      x1[i] = M_PI*i/(double)n*xScale + xOffset;
      y1[i] = r1*qCos(x[i]+2.0)*yScale + yOffset;
    }
    this->customPlot->addGraph();
    this->customPlot->graph(1)->setData(x1, y1);
    this->customPlot->graph(1)->setLineStyle(QCPGraph::lsLine);
    this->customPlot->graph(1)->setPen(QPen(Qt::green));
//    this->customPlot->graph(1)->setBrush(Qt::green);

    yScale = 0.5;
    yOffset = 5;
    QVector<double> x2(n), y2(n);
    for (i=0; i<n; i++)
    {
      x2[i] = M_PI*i/(double)n*xScale + xOffset;
      y2[i] = r1*qCos(x[i]+0.5)*yScale + yOffset;
    }
    this->customPlot->addGraph();
    this->customPlot->graph(2)->setData(x2, y2);
    this->customPlot->graph(2)->setLineStyle(QCPGraph::lsLine);
    this->customPlot->graph(2)->setPen(QPen(Qt::red));
//    this->customPlot->graph(2)->setBrush(Qt::red);

    customPlot->addPlottable( new QCPCurve(customPlot->xAxis, customPlot->yAxis) );
    double a, pha;
    QVector<double> x3(n), y3(n);
    for (i=0; i<n; ++i)
    {
      a = 1.0*(qrand()%10)/10.0;
      pha = i/(1.0*n)*2*M_PI;
      x3[i] = a*qCos(pha);
      y3[i] = a*qSin(pha);
    }
    ((QCPCurve*)customPlot->plottable())->setData(x3, y3);
    ((QCPCurve*)customPlot->plottable())->setPen(QPen(Qt::black));

    customPlot->axisRect()->setupFullAxesBox();
    this->customPlot->rescaleAxes();
    this->customPlot->replot();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::markerToggled(bool checked)
{
    QString buttonText = ((QPushButton*)sender())->text();
    bool ok;
    int index = buttonText.mid(3).toInt(&ok, 10) - 1;
    if(!ok)
    {
        qDebug() << "convert to index failed";
        return;
    }

    if(index>=0&&index<8)
    {
        if(checked)
        {
            this->customPlot->addMarker(index, this->mMkrToTraceSB->value(), false);
        }
        else
        {
            this->customPlot->removeMarker(index);
        }
    }
    else if(index < MARKERNUM)
    {
        if(checked)
        {
            this->customPlot->addMarker(index, this->mMkrToTraceSB->value(), true, 0);

        }
        else
        {
            this->customPlot->removeMarker(index);
        }
    }
    else
    {
        qDebug() << "wrong marker index";
        return;
    }
}

void MainWindow::mkrTxtToggled(bool checked)
{
    this->customPlot->SetMarkerTextEn(checked);
    this->customPlot->replot();
}

void MainWindow::logAxisToggled(bool checked)
{
    if(checked)
    {
        this->customPlot->xAxis->setScaleType(QCPAxis::stLogarithmic);
    }
    else
    {
        this->customPlot->xAxis->setScaleType(QCPAxis::stLinear);
    }
    this->customPlot->replot();
}
